package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/report")
public class ReportController {

	@Reference
	MemberService memberService;

	@Reference
	SetmealService setmealService;

	@Reference
	ReportService reportService;


	@RequestMapping("/exportBusinessReport")
	public Result exportBusinessReport(HttpServletResponse response) throws Exception {

		Map<String, Object> map = reportService.getBusinessReportData();

		String reportDate = String.valueOf(map.get("reportDate"));
		String todayNewMember = String.valueOf(map.get("todayNewMember"));
		String totalMember = String.valueOf(map.get("totalMember"));
		String thisWeekNewMember = String.valueOf(map.get("thisWeekNewMember"));
		String thisMonthNewMember = String.valueOf(map.get("thisMonthNewMember"));
		String todayOrderNumber = String.valueOf(map.get("todayOrderNumber"));
		String todayVisitsNumber = String.valueOf(map.get("todayVisitsNumber"));
		String thisWeekOrderNumber = String.valueOf(map.get("thisWeekOrderNumber"));
		String thisWeekVisitsNumber = String.valueOf(map.get("thisWeekVisitsNumber"));
		String thisMonthOrderNumber = String.valueOf(map.get("thisMonthOrderNumber"));
		String thisMonthVisitsNumber = String.valueOf(map.get("thisMonthVisitsNumber"));



		Object hotSetmealObj = map.get("hotSetmeal");

		List<Map<String,Object>> hotSetmeal = (List<Map<String,Object>>)hotSetmealObj;




		InputStream inputStream = ReportController.class.getResourceAsStream("/template/report_template.xlsx");

		XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

		XSSFSheet sheet = workbook.getSheetAt(0);

		//1设置统计时间
		XSSFRow row = sheet.getRow(2);
		XSSFCell cell = row.getCell(5);
		cell.setCellValue(reportDate);


		//2设置统计时间
		 row = sheet.getRow(4);
		 cell = row.getCell(5);
		cell.setCellValue(todayNewMember);

//3
		row = sheet.getRow(4);
		cell = row.getCell(7);
		cell.setCellValue(totalMember);
//4
		row = sheet.getRow(5);
		cell = row.getCell(5);
		cell.setCellValue(thisWeekNewMember);
//5
		row = sheet.getRow(5);
		cell = row.getCell(7);
		cell.setCellValue(thisMonthNewMember);
//6
		row = sheet.getRow(7);
		cell = row.getCell(5);
		cell.setCellValue(todayOrderNumber);
//7
		row = sheet.getRow(7);
		cell = row.getCell(7);
		cell.setCellValue(todayVisitsNumber);
//8
		row = sheet.getRow(8);
		cell = row.getCell(5);
		cell.setCellValue(thisWeekOrderNumber);
//9
		row = sheet.getRow(8);
		cell = row.getCell(7);
		cell.setCellValue(thisWeekVisitsNumber);
//10
		row = sheet.getRow(9);
		cell = row.getCell(5);
		cell.setCellValue(thisMonthOrderNumber);
//11
		row = sheet.getRow(9);
		cell = row.getCell(7);
		cell.setCellValue(thisMonthVisitsNumber);

		//12设置热门套餐

		int rownum = 12;
		for (Map<String,Object> setmeal:hotSetmeal
			 ) {
			row = sheet.getRow(rownum);

			cell = row.getCell(4);
			Object nameObj = setmeal.get("name");
			cell.setCellValue(String.valueOf(nameObj));

			cell = row.getCell(5);
			Object setmeal_count = setmeal.get("setmeal_count");
			cell.setCellValue(String.valueOf(setmeal_count));

			cell = row.getCell(6);
			Object proportion = setmeal.get("proportion");
			cell.setCellValue(String.valueOf(proportion));

			rownum++;
		}


		//将excel下载到本地
		//4. 下载excel到本地
		// 通过输出流进行文件下载
		ServletOutputStream out = response.getOutputStream();
		//指定附件的格式为excel
		response.setContentType("application/vnd.ms-excel");
		//attachment 以附件下载
		//下载弹出框架中的默认的文件名
		response.setHeader("content-Disposition","attachment;filename="+reportDate+"_report.xlsx");
		//输出流下载
		workbook.write(out);
		out.flush();
		out.close();
		workbook.close();

		return  null;
	}



	@RequestMapping("/getBusinessReportData")
	public Result getBusinessReportData() throws Exception {
		log.debug("getBusinessReportData");
		Map<String,Object> map = reportService.getBusinessReportData();
		log.debug("运营数据统计结果:"+map.toString());
		return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS,map);
	}


	@RequestMapping("/getMemberReport")
	public Result getMemberReport(){

		Map<String,Object> map = memberService.getMemberReport();


		return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

	}

	@RequestMapping("/getSetmealReport")
	public Result getSetmealReport(){

		try{
			// 获取套餐占比数据
			List<Map<String,Object>> setmealCountList = setmealService.findSetmealCount();
			// 获取套餐名称
			List<String> setmealNames = new ArrayList<>();
			for (Map<String,Object> mapSetmeal:setmealCountList){
				String setmealName = (String)mapSetmeal.get("name");
				setmealNames.add(setmealName);
			}
			// 构建返回数据
			Map<String,Object> mapData = new HashMap<>();
			mapData.put("setmealCount",setmealCountList);
			mapData.put("setmealNames",setmealNames);
			return new Result(true,MessageConst.ACTION_SUCCESS,mapData);

		}catch(Exception e){
			e.printStackTrace();
			return new Result(false,MessageConst.ACTION_FAIL);
		}



	}


}
